package com.zy.datareceiver.core.utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;

public class RabbitMqUtil {
    @Value("${spring.rabbitmq.host}")
    private static final String host = "127.0.0.1";
    @Value("${spring.rabbitmq.port}")
    private static final String port = "5672";
    @Value("${spring.rabbitmq.username}")
    private static final String username = "admin";
    @Value("${spring.rabbitmq.password}")
    private static final  String password = "123456";


    private static volatile Connection connection = null;

    public static synchronized Connection creatConnetion()throws Exception{
        if (connection == null){
            ConnectionFactory factory =new ConnectionFactory();
            factory.setHost(host);
            factory.setUsername(username);
            factory.setPassword(password);
            factory.setPort(Integer.parseInt(port));
            connection = factory.newConnection();
        }
        return connection;
    }
}
